/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.resource;

import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * @author kman
 *
 */
public class TrashResource extends RestResource{

	public TrashResource(String aUri) {
		super(aUri);
	}

	List<String> filePaths = new LinkedList<String>();
	List<String> subfolderPaths = new LinkedList<String>();
	List<FolderResource> folders = new ArrayList<FolderResource>();
	List<FileResource> files = new ArrayList<FileResource>();

	/**
	 * Retrieve the file paths.
	 *
	 * @return the file paths
	 */
	public List<String> getFilePaths() {
		return filePaths;
	}

	/**
	 * Modify the file paths.
	 *
	 * @param newFilePaths the file paths to set
	 */
	public void setFilePaths(List<String> newFilePaths) {
		filePaths = newFilePaths;
	}

	/**
	 * Retrieve the subfolder paths.
	 *
	 * @return the subfolder paths
	 */
	public List<String> getSubfolderPaths() {
		return subfolderPaths;
	}

	/**
	 * Modify the subfolder paths.
	 *
	 * @param newSubfolderPaths the subfolders to set
	 */
	public void setSubfolderPaths(List<String> newSubfolderPaths) {
		subfolderPaths = newSubfolderPaths;
	}

	/**
	 * Retrieve the folders.
	 *
	 * @return the folders
	 */
	public List<FolderResource> getFolders() {
		return folders;
	}

	/**
	 * Modify the folders.
	 *
	 * @param newFolders the folders to set
	 */
	public void setFolders(List<FolderResource> newFolders) {
		folders = newFolders;
	}

	/**
	 * Retrieve the files.
	 *
	 * @return the files
	 */
	public List<FileResource> getFiles() {
		return files;
	}

	/**
	 * Modify the files.
	 *
	 * @param newFiles the files to set
	 */
	public void setFiles(List<FileResource> newFiles) {
		files = newFiles;
	}

	@Override
	public void createFromJSON(String text) throws JSONException {
		JSONObject json = new JSONObject(text);
		JSONArray subs = getJSONArray(json, "folders");
		Date creationDate=null;
		Date modificationDate=null;
		if (subs != null)
			for (int i = 0; i < subs.length(); i++) {
				JSONObject so = subs.getJSONObject(i);
				if (so != null) {
					String subUri = unmarshallString(so, "uri");
					String subName = unmarshallString(so, "name");
					String testString = unmarshallString(so, "creationDate");
					if (testString != null)
						creationDate = new Date(new Long(testString));
					testString = unmarshallString(so, "modificationDate");
					if (testString != null)
						modificationDate = new Date(new Long(testString));
					if (subUri != null && subName != null) {
						if (!subUri.endsWith("/"))
							subUri = subUri + "/";
						FolderResource sub = new FolderResource(subUri);
						sub.setName(subName);
						sub.setParentURI(uri);
						sub.setNeedsExpanding(true);
						folders.add(sub);
						subfolderPaths.add(subUri);
					}
				}
			}
		subs = getJSONArray(json, "files");
		if (subs != null)
			for (int i = 0; i < subs.length(); i++) {
				JSONObject fo = subs.getJSONObject(i);
				if (fo != null) {
					String fname = unmarshallString(fo, "name");
					String fowner = unmarshallString(fo, "owner");
					String fcontent = unmarshallString(fo, "content");
					String fpath = unmarshallString(fo, "path");
					fpath = URLDecoder.decode(fpath);
					Integer fversion = null;
					String versionString = unmarshallString(fo, "version");
					if (versionString != null)
						fversion = new Integer(versionString);
					boolean fdeleted = unmarshallBoolean(fo, "deleted");
					String testString = unmarshallString(fo, "creationDate");
					Date fcreationDate = null;
					Date fmodificationDate = null;
					if (testString != null)
						fcreationDate = new Date(new Long(testString));
					testString = unmarshallString(fo, "modificationDate");
					if (testString != null)
						fmodificationDate = new Date(new Long(testString));
					String furi = unmarshallString(fo, "uri");
					Long fsize = 0L;
					testString = unmarshallString(fo, "size");
					if (testString != null)
						fsize = new Long(testString);
					filePaths.add(furi);
					FileResource fs = new FileResource(furi);
					fs.setName(fname);
					fs.setOwner(fowner);
					fs.setPath(fpath);
					fs.setVersion(fversion);
					fs.setContentLength(fsize);
					fs.setDeleted(fdeleted);
					fs.setCreationDate(fcreationDate);
					fs.setModificationDate(fmodificationDate);
					fs.setContentType(fcontent);
					files.add(fs);
				}
			}
		/*
		if (json.get("folders") != null) {
			JSONArray subs = json.get("folders").isArray();
			if (subs != null)
				for (int i = 0; i < subs.size(); i++) {
					JSONObject so = subs.get(i).isObject();
					if (so != null) {
						String subUri = unmarshallString(so, "uri");
						String subName = unmarshallString(so, "name");
						subName = URL.decodeComponent(subName);
						if (subUri != null && subName != null) {
							if (!subUri.endsWith("/"))
								subUri = subUri + "/";
							FolderResource sub = new FolderResource(subUri);
							sub.setName(subName);
							sub.setNeedsExpanding(true);
							folders.add(sub);
							subfolderPaths.add(subUri);
						}
					}
				}
		}
		if (json.get("files") != null) {
			JSONArray subs = json.get("files").isArray();
			if (subs != null)
				for (int i = 0; i < subs.size(); i++) {
					JSONObject fo = subs.get(i).isObject();
					if (fo != null) {
						String fname = unmarshallString(fo, "name");
						String fowner = unmarshallString(fo, "owner");
						String fcontent = unmarshallString(fo, "content");
						String fpath = unmarshallString(fo, "path");
						fpath = URL.decodeComponent(fpath);
						Integer fversion = null;
						if (fo.get("version") != null)
							fversion = new Integer(fo.get("version").toString());
						boolean fdeleted = unmarshallBoolean(fo, "deleted");
						Date fcreationDate = null;
						if (fo.get("creationDate") != null)
							fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
						Date fmodificationDate = null;
						if (fo.get("modificationDate") != null)
							fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
						String furi = unmarshallString(fo,"uri");
						Long fsize = 0L;
						if(fo.get("size") != null)
							fsize = new Long(fo.get("size").toString());
						filePaths.add(furi);
						FileResource fs = new FileResource(furi);
						fs.setName(fname);
						fs.setOwner(fowner);
						fs.setVersion(fversion);
						fs.setContentLength(fsize);
						fs.setDeleted(fdeleted);
						fs.setPath(fpath);
						fs.setCreationDate(fcreationDate);
						fs.setModificationDate(fmodificationDate);
						fs.setContentType(fcontent);
						files.add(fs);
					}
				}
		}*/
	}
	/*
	public List<FolderResource> getTrashedFolders(){
		List<FolderResource> res = new ArrayList<FolderResource>();
		for(String s : subfolderPaths){
			String[] pathElements =  s.split("/");
			FolderResource tr = new FolderResource(s);
			tr.setName(URL.decodeComponent(pathElements[pathElements.length-1]));
			res.add(tr);
		}
		return res;
	}*/

	@Override
	public String getLastModifiedSince() {
		return null;
	}
	/*@Override
	public String constructUri(TreeItem treeItem, String path){
		String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1);
		return constructedUri;
	}*/
}
